package com.sqpoll.android.group;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.sqpoll.android.R;

/**
 * The adapter for group list.
 * 
 * @author Younggue Bae
 */
public class GroupListAdapter extends BaseAdapter {

	public static final String TAG = "GroupListAdapter";
	
	Context mContext;
	LayoutInflater mInflater;
	int mLayoutResourceId;
	private List<Group> mRowList;

	public GroupListAdapter(Context context, int layoutResourceId) {
		this(context, layoutResourceId, new ArrayList<Group>());
	}
	
	public GroupListAdapter(Context context, int layoutResourceId, List<Group> items) {
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mContext = context;
		this.mLayoutResourceId = layoutResourceId;
		this.mRowList = items;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_item_group, null);
			
			holder.mImageGroup = (ImageView) convertView.findViewById(R.id.image_group);
			holder.mGroupName = (TextView) convertView.findViewById(R.id.group_name);
			holder.mLatesetPoll = (TextView) convertView.findViewById(R.id.latest_poll);
			holder.mNewPollCount = (TextView) convertView.findViewById(R.id.new_poll_count);
			holder.mButtonEdit = (Button) convertView.findViewById(R.id.button_edit);
			holder.mButtonExit = (Button) convertView.findViewById(R.id.button_exit);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		Group item = (Group) getItem(position);
		
		holder.mImageGroup.setImageDrawable(item.getGroupImage());
		holder.mGroupName.setText(item.getGroupName());
		holder.mLatesetPoll.setText(item.getLatestPoll());
		holder.mNewPollCount.setText(String.valueOf(item.getNewPollCount()));
		
		holder.mButtonEdit.setOnClickListener(mEditButtonClickListener);
		holder.mButtonExit.setOnClickListener(mExitButtonClickListener);

		return convertView;
	}
	
	OnClickListener mEditButtonClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			GroupEditDialogFragment dialog = new GroupEditDialogFragment();
			Log.d(TAG, "Click: Edit group button");
			
			FragmentActivity activity = (FragmentActivity) mContext;
			dialog.show(activity.getSupportFragmentManager(), "GroupEditDialogFragment");
    }
	};
	
	OnClickListener mExitButtonClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			GroupExitDialogFragment dialog = new GroupExitDialogFragment();
			Log.d(TAG, "Click: Exit group button");
			
			FragmentActivity activity = (FragmentActivity) mContext;
			dialog.show(activity.getSupportFragmentManager(), "GroupExitDialogFragment");
    }
	};
	
	@Override
	public int getCount() {
		return mRowList.size();
	}

	@Override
	public Object getItem(int position) {
		return mRowList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void addItem(Group item) {
		mRowList.add(item);
	}
	
	public void addItems(List<Group> items) {
		mRowList.addAll(items);
	}
	
	/**
	 * View holder for a list item.
	 * 
	 */
	class ViewHolder {

		ImageView mImageGroup;
		TextView mGroupName;
		TextView mLatesetPoll;
		TextView mNewPollCount;
		Button mButtonEdit;
		Button mButtonExit;
	}

}